﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.ParameterRebinder
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System.Collections.Generic;
using System.Linq.Expressions;

#nullable enable
namespace Commom.Util;

public class ParameterRebinder : ExpressionVisitor
{
  private readonly Dictionary<ParameterExpression, ParameterExpression> _map;

  public ParameterRebinder(
    Dictionary<ParameterExpression, ParameterExpression> map)
  {
    this._map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
  }

  public static Expression ReplaceParameters(
    Dictionary<ParameterExpression, ParameterExpression> map,
    Expression exp)
  {
    return new ParameterRebinder(map).Visit(exp);
  }

  protected override Expression VisitParameter(ParameterExpression parameterExpression)
  {
    ParameterExpression parameterExpression1;
    if (this._map.TryGetValue(parameterExpression, out parameterExpression1))
      parameterExpression = parameterExpression1;
    return base.VisitParameter(parameterExpression);
  }
}
